<?php

/**
 * This is the model class for table "usuario".
 *
 * The followings are the available columns in table 'usuario':
 * @property string $IDUSUARIO
 * @property string $Nick
 * @property integer $ESTADO
 * @property string $NOMBRE
 * @property string $APELLIDO
 * @property string $Contraseña
 * @property integer $Intentos
 * @property string $Rol
 *
 * The followings are the available model relations:
 * @property Facturas[] $facturases
 * @property Productos[] $productoses
 */
class Usuario extends CActiveRecord
{
	public $r;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Usuario the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'usuario';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Nick, NOMBRE, APELLIDO, Contraseña, Rol, Descuentos', 'required', 'message'=>'Campos requeridos'),
			array('ESTADO, Intentos, Rol, IDUSUARIO, Descuentos', 'numerical','integerOnly'=>true,'message'=>'Es un campo numérico'),
			array('NOMBRE', 'length', 'max'=>20),
			array('Nick', 'length', 'max'=>10),
			array('APELLIDO', 'length', 'max'=>30),
			array('Contraseña', 'length', 'max'=>40),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('IDUSUARIO, Nick, ESTADO, NOMBRE, APELLIDO, Contraseña, Intentos, Rol, r', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'facturases' => array(self::HAS_MANY, 'Facturas', 'IDUSUARIO'),
			'productoses' => array(self::HAS_MANY, 'Productos', 'IDUSUARIO'),
			'rol' => array(self::BELONGS_TO, 'Rol', 'Rol'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'IDUSUARIO' => 'Id de Usuario',
			'Nick' => 'Usuario',
			'ESTADO' => 'Estado',
			'NOMBRE' => 'Nombre',
			'APELLIDO' => 'Apellido',
			'Rol'=>'Rol',
			'Descuentos'=>'Descuentos',
			'r'=> 'Rol',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->with = array('rol');
        $criteria->addSearchCondition('rol.Rol', $this->r);
		$criteria->compare('Nick',$this->Nick,true);
		$criteria->compare('ESTADO',$this->ESTADO);
		$criteria->compare('NOMBRE',$this->NOMBRE,true);
		$criteria->compare('APELLIDO',$this->APELLIDO,true);
		//$criteria->compare('Rol',$this->Rol,true);

		$_SESSION['datos_filtrados'] = new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
                'pagination'=>false,
        ));

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}


public function validatePassword($pass){
return $this->hashPassword($pass)===$this->Contraseña;
}

public function hashPassword($pass){
return md5($pass);
}
}